package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewSysLogininforService() SysLogininforService {
	return &sysLogininforService{
		SysLogininforRepo: repository.NewSysLogininforRepository(),
	}
}

type sysLogininforService struct {
	SysLogininforRepo *repository.SysLogininforRepository
}

// GetById 根据id获取配置信息
func (s *sysLogininforService) GetById(infoId int64) (*vo.SysLogininforModel, error) {
	m, err := s.SysLogininforRepo.GetById(infoId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysLogininforModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

func (s *sysLogininforService) List(req *vo.QuerySysLogininforReq) (*vo.ListSysLogininforRsp, error) {

	pageReq := request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	list, total, err := s.SysLogininforRepo.List(req, &pageReq)

	if err != nil {
		return nil, err
	}

	itemList := make([]*vo.SysLogininforModel, 0)
	for _, v := range list {
		item := new(vo.SysLogininforModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysLogininforRsp{
		List:  itemList,
		Total: total,
	}, nil
}

func (s *sysLogininforService) Add(req *vo.AddSysLogininforReq) (*vo.AddSysLogininforRsp, error) {
	m := new(model.SysLogininfor)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysLogininforRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysLogininforRsp{
		InfoId: m.InfoId,
	}, nil
}
func (s *sysLogininforService) Edit(req *vo.EditSysLogininforReq) (*vo.EditSysLogininforRsp, error) {
	m := new(model.SysLogininfor)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysLogininforRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysLogininforRsp{
		InfoId: req.InfoId,
	}, nil
}

func (s *sysLogininforService) Del(infoId int64) error {
	return s.SysLogininforRepo.Delete(infoId)
}

func (s *sysLogininforService) BatchDel(ids []string) error {
	return s.SysLogininforRepo.DeleteBatch(ids)
}

func (s *sysLogininforService) Clean() error {
	return s.SysLogininforRepo.Clean()
}
